package com.cgnpc.scp.manager.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.manager.mybatis.entity.MessageTemplate;
import com.cgnpc.scp.manager.service.IMessageTemplateService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/******************************
 * 用途说明: 消息模板
 * 作者姓名: chenshijie
 * 创建时间: 2020/08/11 09:50
 ******************************/
@Controller
@RequestMapping("/scp/manager/msgtpl")
public class MessageController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    IMessageTemplateService messageTemplateService;

    /**********************************
     * 用途说明: 保存消息模板
     * 参数说明 messageTemplate
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="保存消息模板")
    @UBA(module = "系统管理", action = "保存消息模板", channel = OperatorType.Button)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult save(HttpServletRequest request, @RequestBody MessageTemplate messageTemplate) {
        try{
            int updateRows = messageTemplateService.save(messageTemplate);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,updateRows);
        }catch (Exception e){
            LOGGER.error("MessageController save error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 更新消息模板
     * 参数说明 messageTemplate
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="更新消息模板")
    @UBA(module = "系统管理", action = "编辑消息模板", channel = OperatorType.Button)
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult update(HttpServletRequest request, @RequestBody MessageTemplate messageTemplate) {
        try{
            int updateRows = messageTemplateService.update(messageTemplate);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,updateRows);
        }catch (Exception e){
            LOGGER.error("MessageController update error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 根据id获取消息模板
     * 参数说明 messageTemplate
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="根据id获取消息模板")
    @UBA(module = "系统管理", action = "查看消息模板详情", channel = OperatorType.Button)
    @RequestMapping(value = "/get", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult get(HttpServletRequest request, @RequestBody MessageTemplate messageTemplate) {
        try{
            MessageTemplate p = messageTemplateService.get(messageTemplate);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,p);
        }catch (Exception e){
            LOGGER.error("MessageController get error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 根据id删除消息模板
     * 参数说明 ids
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="根据id删除消息模板")
    @UBA(module = "系统管理", action = "删除消息模板", channel = OperatorType.Button)
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult delete(HttpServletRequest request, @RequestBody List<String> ids) {
        try{
            int delete = messageTemplateService.delete(ids);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,delete);
        }catch (Exception e){
            LOGGER.error("MessageController delete error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 分页查询消息模板
     * 参数说明 messageTemplate
     * 返回值说明:
     ***********************************/
    @ApiOperation(value="分页查询消息模板")
    @UBA(module = "系统管理", action = "消息模板分页查询", channel = OperatorType.Page)
    @RequestMapping(value = "/loadByPaging", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult loadByPaging(HttpServletRequest request, @RequestBody MessageTemplate messageTemplate) {
        try{
            MyPageResult<MessageTemplate> processInfoMyPageResult = messageTemplateService.loadByPaging(messageTemplate);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,processInfoMyPageResult);
        }catch (Exception e){
            LOGGER.error("MessageController loadByPaging error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

}
